<template>
  <div class="echart" ref="mychart" id="mychart" :style="{ width: '100%', height: '400px' }"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'testView',
  data() {
    return {
      option: {
        tooltip: {
          trigger: 'item',
        },
        legend: {
          top: '5%',
          left: 'center',
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            padAngle: 5,
            itemStyle: {
              borderRadius: 10,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 1048, name: 'Search Engine' },
              { value: 735, name: 'Direct' },
              { value: 580, name: 'Email' },
              { value: 484, name: 'Union Ads' },
              { value: 300, name: 'Video Ads' },
            ],
          },
        ],
      },
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      // 获取 DOM 元素，用于初始化图表
      const myChart = echarts.init(this.$refs.mychart)

      // 设置图表的配置项
      myChart.setOption(this.option)

      // 监听窗口调整大小，调整图表的大小
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    },
  },
}
</script>

<style scoped>
/* 设置图表容器的样式 */
.echart {
  width: 100%;
  height: 400px;
}
</style>
